<?php
header("Content-Type:text/html;charset=utf-8");
error_reporting(E_ERROR | E_PARSE);
session_start();
include_once 'includes/init.php';
include_once 'includes/upload.class.php';
include_once 'includes/send.php';
// print_r($_SESSION);
$id=isset($_SESSION['id'])?$_SESSION['id']:'';
$nphone = isset($_SESSION['phone']) ? $_SESSION['phone'] : '';
$uesrname  =isset($_SESSION['uesrname']) ? $_SESSION['uesrname'] : '';
$sql="select * from t_member where id=$id";
$info=$db_Obj->getone($sql);
$_SESSION['rate'] = $info['rate'];
$project = $_GET['project'];


$sql="select* from t_product  where lasttime>".time()."   and project='".$project."' ";
$infoall=$db_Obj->getall($sql);

// 检测是否竞价
$sqls = "select * from t_product where lasttime> ".time()." and project = '".$project."' and status = 0 ";
$results = $db_Obj ->getall($sqls);
if(!empty($results)){
	// 非竞价
	$nobid = 1;

}

if(empty($project)){
	echo "<script>alert('您暂无可查看项目!');window.history.go(-1);</script>";
	return false;
}

// unset($_POST['data']);
if (!empty($_POST)) {
	global $nobid;

	if(!empty($nobid)){
		// 一个供应商 不可以 对 该产品提交相同的价格
		// print_r($_POST);
		// 先去检测这个供应商提交的该产品价格是否存在
		for ($i=0; $i <count($_POST['type']) ; $i++) { 
			# code...
			$sql = "select * from t_bid  where mid = '".$id."' and btype = '".$_POST['type'][$i]."' ";
			$result = $db_Obj->getall($sql);
		
			foreach ($result as $key => $value) {
				if($_POST['price'][$i] == $value['price']){
					echo "<script>alert('抱歉,该产品价格已存在!');window.history.go(-1);</script>";
					die();
				}
				if($_POST['price'][$i] > $value['price']){
					echo "<script>alert('抱歉,价格不可以大于上次提交价格');window.history.go(-1);</script>";
					die();
				}
			}
		}
	}


	$send_title = '报价';
	$body = '
		<!DOCTYPE html>
			<html lang="en">
			<head>
				<meta charset="UTF-8">
				<title>baojia</title>
			</head>
			<body>
				<h2>您好,供应商&nbsp;『'."$uesrname".'』已提交报价,请查看!</h2>
			</body>
		</html>
	';
 	$tarr = array(
	 		'filepath'=>'upload',
	 		'maxsize'=>'100',
	 		'allowmime'=>array(
				'image/jpeg',
                'image/jpg',
                'image/pjpeg',
                'image/gif',
                'image/png',
                'image/x-png',
                'application/msexcel',
                'application/vnd.ms-excel',
                'application/msword',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'text/plain',
            ),
            'israndname'=>'1',
        );

	$upObj = new fileup($tarr); 
 	$filename = $upObj->up('file_name');
 	if ($filename == '') { // 若文件名为空则输出错误信息
 		echo 'msg= '.$upObj->geterror();
 	}else {
 		$_POST['file_name'] = $filename;
 	}

    $type = $_POST['type'][0];

	$sql = "select * from t_product where type = '".$type."' ";
	$query = $db_Obj->getall($sql);
		
	$newPrice = $_POST['price'][0];
	// var_dump($newPrice);
	// var_dump($query);die;
	$oldPrice = (int)$query[0]['price'];

	if($oldPrice == 0){
		for ($i=0; $i <count($_POST['type']) ; $i++) { 
				if(empty($_POST['price'])){
					continue;
				}
				$time=time();
				$sql2 = "INSERT INTO t_bid (mid,price,createtime,btype,bsize,bnumber,rate,delivery,MOQ,MPQ,file_name,status,note) VALUES (".$id.",'".$_POST['price'][$i]."',".$time.",'".$_POST['type'][$i]."','".$_POST['size'][$i]."',".$_POST['number'][$i].",'".$_POST['rate'][$i]."','".$_POST['delivery'][$i]."','".$_POST['MOQ'][$i]."','".$_POST['MPQ'][$i]."','".$_POST['file_name']."',0,'".$_POST['note'][$i]."')";
					$db_Obj->query($sql2);

				
				if($query[0]['status'] == 1){
					$datas = array('price'=>$newPrice);
					$query = $db_Obj->update('t_product', $datas, "type='{$type}'");
				}
			}
		$a = sendemail('baojiaxt@163.com','你',$send_title,$body );

		
		// 参数说明(收件人邮箱,收件人姓名, 邮件主题, 邮件内容, 附件 )
		echo "<script>alert('添加成功');window.history.go(-1);</script>";
	}else{
		if($newPrice >= $oldPrice){
				echo "<script>alert('抱歉,此次报价价格,必须小于上次报价价格.');window.history.go(-1);</script>";
		}else{
			for ($i=0; $i <count($_POST['type']) ; $i++) { 
				if(empty($_POST['price'][$i])){
					continue;
				}
				$time=time();
				$sql2 = "INSERT INTO t_bid (mid,price,createtime,btype,bsize,bnumber,rate,delivery,MOQ,MPQ,file_name,status,note) VALUES (".$id.",'".$_POST['price'][$i]."',".$time.",'".$_POST['type'][$i]."','".$_POST['size'][$i]."',".$_POST['number'][$i].",'".$_POST['rate'][$i]."','".$_POST['delivery'][$i]."','".$_POST['MOQ'][$i]."','".$_POST['MPQ'][$i]."','".$_POST['file_name']."',1,'".$_POST['note'][$i]."')";
						$db_Obj->query($sql2);
				
				if($query[0]['status'] == 1){
					$datas = array('price'=>$newPrice);
					$query = $db_Obj->update('t_product', $datas, "type='{$type}'");
				}
			}
			$a = sendemail('baojiaxt@163.com','你',$send_title,$body );
			// var_dump($a);die;
			// 参数说明(收件人邮箱,收件人姓名, 邮件主题, 邮件内容, 附件 )
			echo "<script>alert('添加成功');window.history.go(-1);</script>";
		}
	}	
}	

// echo $sqlv;
// print_r($results);
// 产品数据
// $sql="select * from t_product where project ='".$project."' order by pid desc ";
// $result = $db_Obj->getall($sql);
// ajax 联动
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		body{width: 1200px;margin:0 auto;}
		header{height: 120px;line-height: 120px;background: url(123.png);width: 100%}
		.mydetail{margin-top: 20px;}
		.bid_title{padding-left: 20px;font-weight: bold;height: 30px;line-height: 30px;margin: 30px 0;}
		table {margin-top:20px;width: 100%;	background:#fff;border-collapse: collapse;border-spacing: 0;}
		.mydetail{padding-bottom: 50px;border-bottom: 1px dashed #000}
		.mydetail th, .mydetail td {border: 1px solid #e2e2e2;text-align: center;	padding: 10px 5px;}
		.bid td{border: 1px solid #e2e2e2;text-align: center;background: #fff;}
		.bid th{background: #eee;padding: 10px 5px;border: 1px solid #e2e2e2;text-align: center;}
		.tal{text-align: left;}
		.tac{text-align: center;}
		.bid_con{margin-bottom: 30px}
		tr.selected{background: #eee}
		input[type="text" i]:disabled{background:#f7f7f7;}
		.select_type{border:none;width: 140px;height: 30px;outline: none}
		.chos{width: 20px;height: 20px}
		input.long{margin:8px 4px;height: 30px;line-height: 30px;width: 80%;padding-left: 30px;border:1px solid #ddd;outline: none}
		input.long1{margin:0;width: 100%;border:none;outline: none;height: 30px;text-align: center}
		input.btn{margin-right: 30px}
		.info_ti{width: 80px;display: inline-block;margin-left: 20px}
    .STYLE3 {font-size: 14px}
    .STYLE4 {text-align: center; color: #FFFFFF; }
    a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
</style>
	<script src="jquery-1.7.2.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
		<table width="1200" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="100"><a href="http://www.ego-buy.com.cn/"><img src="image/bg11.jpg" width="1200" height="76" border="0"></a></td>
          </tr>
          <tr>
            <td height="87" background="image/bg10.jpg"><h2 class="STYLE4"><?php echo $_GET['project']; ?></h2></td>
          </tr>
          <tr>
            <td height="50">		<a href="index.php" style="margin-left:10px;">返回列表</a></td>
          </tr>
        </table>
		<div class="mydetail">		
		<table border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
			<tbody>
				<tr style="background: #eee">
					<th width="16%"><span class="STYLE3">物料编码</span></th>
					<th width="13%"><span class="STYLE3">规格参数</span></th>
					<th width="7%"><span class="STYLE3">数量</span></th>
					<th width="8%"><span class="STYLE3">计量单位</span></th>
					<th width="7%"><span class="STYLE3">币种</span></th>
					<th width="13%"><span class="STYLE3">截至日期</span></th>
					<th width="10%"><span class="STYLE3">目前最低价格</span></th>
					<th width="9%"><span class="STYLE3">附件</span></th>
					<th width="17%"><span class="STYLE3">备注</span></th>
				</tr>
				<?php for ($i=0; $i <count($infoall) ; $i++) {   ?>
				<tr>

					<td><span class="STYLE3"><?php echo $infoall[$i]['type'] ?></span></td>
					<td><span class="STYLE3"><?php echo $infoall[$i]['size'] ?></span></td>
					<td><span class="STYLE3"><?php echo $infoall[$i]['number'] ?></span></td>
					<td><span class="STYLE3"><?php echo $infoall[$i]['units'] ?></span></td>
					<td><span class="STYLE3"><?php echo $infoall[$i]['currency'] ?></span></td>
					<td><span class="STYLE3"><?php echo date('Y-m-d H:i:s',$infoall[$i]['lasttime']) ?></span></td>
					<td><span class="STYLE3">
				    <?php if($infoall[$i]['status'] == 1){echo $infoall[$i]['price'];}else{echo "--";} ?>
					</span></td>
					<td><a href="./<?php echo $infoall[$i]['file_name']; ?>" class="STYLE3" download="./<?php echo $infoall[$i]['file_name']; ?>"><?php echo substr($infoall[$i]['file_name'], 7) ?></a></td>
					<td><span class="STYLE3"><?php echo $infoall[$i]['note'] ?></span></td>
				</tr>
			<?php } ?>
			</tbody>
		</table>
</div>
		<div class="bid">
		<div class="bid_title">报价表</div>
		<div class="bid_con">
			<form action="" method="post" onSubmit=" return pw()" enctype="multipart/form-data">
				<table width="1200" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><span class="STYLE3">公司名称：</span></td>
                    <td align="left"><input name="data[company_name]" type="text" class="long " value="<?php echo $info['company_name'] ?>"></td>
                    <td align="right"><span class="STYLE3">联系人：</span></td>
                    <td align="left"><input name="data[names]" type="text" class="long " value="<?php echo $info['names'] ?>"></td>
                    <td align="right"><span class="STYLE3">电话：</span></td>
                    <td align="left"><input name="data[phone]" type="text" class="long " value="<?php echo $info['phone'] ?>"></td>
                    <td align="right"><span class="STYLE3">E-mail</span></td>
                    <td align="left"><input name="data[email]" type="text" class="long " value="<?php echo $info['email'] ?>"></td>
                  </tr>
                </table>
			    <table border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
					<tbody class="table2">
						<tr style="background: #eee">
      <th width="50" ><span class="STYLE3">序号</span></th>
      <th width="160"><span class="STYLE3">物料编码</span></th>
      <th width="130"><span class="STYLE3">规格参数</span></th>
      <th width="80"><span class="STYLE3">币种</span></th>
      <th width="60"><span class="STYLE3">计量单位</span></th>
      <th width="80"><span class="STYLE3">数量</span></th>
      <th width="80"><span class="STYLE3">未税单价</span></th>
      <th width="60"><span class="STYLE3">税率（%）</span></th>
      <th width="80"><span class="STYLE3">交期（天）</span></th>
      <th width="80"><span class="STYLE3">MOQ</span></th>
      <th width="80"><span class="STYLE3">MPQ</span></th>
      <th width="110"><span class="STYLE3">附件</span></th>
      <th width="150"><span class="STYLE3">备注</span></th>
    </tr>
						<?php foreach ($infoall as $key => $value) { ?>
						<tr class="input_table">
							<td class="num"><?php echo $key+1; ?></td>
							<td name="type[]">
								<input class="long1" type="text" readonly="readonly" name="type[]" value="<?php echo $value['type']; ?>" >							</td>
							<td><input class="long1" type="text" readonly="readonly" name="size[]" value="<?php echo $value['size']; ?>"></td>
							<td><input class="long1" type="text" readonly="readonly" name="currency[]" value="<?php echo $value['currency']; ?>" ></td>
							<td><input class="long1" type="text" readonly="readonly" value="<?php echo $value['units']; ?>" ></td>
							<td><input class="long1" type="text" value="<?php echo $value['number']; ?>" name="number[]"></td>
							<td><input class="long1" type="text" name="price[]"></td>
							<td><input class="long1" type="text" readonly="readonly" name="rate[]" value="<?php echo $_SESSION['rate']; ?>"></td>
							<td><input class="long1" name="delivery[]"></td>
							<td><input class="long1" type="text" name="MOQ[]"></td>
							<td><input class="long1" type="text" name="MPQ[]"></td>
							<td><input class="long1" type="file" name="file_name"></td>
							<td><input class="long1" type="text" name="note[]"></td>
						</tr>
						<?php } ?>
					</tbody>
			  </table>
				<!-- <div style="margin-top:20px"><input class="btn" type="button" value="增加行" onclick="addRow()"><input class="btn" type="button" value="删除行" onclick="delRow()"></div>
				<div style="text-align:center"><input style="width: 50px;margin-right:20px;" type="submit" value="提交"><input style="width: 50px;" type="reset" class="reset" value="重置"></div> -->
				
				<div style="margin-top:20px"></div>
				<div style="text-align:center">
                    <input style="width: 50px;margin-right:20px;" type="submit" value="提交">
                    <input style="width: 50px;" type="reset" class="reset" value="重置">
                </div>
				

			</form>
		</div>
		</div>
		<script>
	function pw() {
	    var number = $("input[name='number[]']").val();
	    var price = $("input[name='price[]']").val();
	    var rate = $("input[name='rate[]']").val();
	    var MOQ = $("input[name='MOQ[]']").val();
	    var MPQ = $("input[name='MPQ[]']").val();
			if(number == '')
			{
				$("#tishi").html("<font color='red'>参数不能为空!!!都填满!</font>");  
		        return  false;
			}
			if(price == '')
			{
				$("#tishi").html("<font color='red'>参数不能为空!!!都填满!</font>");  
		        return  false;
			}
			if(rate == '')
			{
				$("#tishi").html("<font color='red'>参数不能为空!!!都填满!</font>");  
		        return  false;
			}
			if(MOQ == '')
			{
				$("#tishi").html("<font color='red'>参数不能为空!!!都填满!</font>");  
		        return  false;
			}
			if(MPQ == '')
			{
				$("#tishi").html("<font color='red'>参数不能为空!!!都填满!</font>");  
		        return  false;
			}
	     return true;
	}
	// $('.reset').trigger('click');
	//判断被选中的行数
	/*var oo = false;
	function num(){
		var checks = $('input[name="check"]');
		var n =0;
		for(i=0;i<checks.length;i++){
			if(checks[i].checked)
			n++;
		}
		if(n < $('tbody.table2>tr').length-1){
			oo = true;
			n=0;
			return oo;
		}
	}
	//增加行
	function addRow () {
		var num = $('tbody.table2>tr').length;
		var addTR = $('tr.input_table:last-child').clone(true);
		addTR.appendTo("tbody.table2");
		addTR.find('input[type="text"]').val('');
		addTR.find('td.num').html(num);
	}
	// 删除行
	function delRow () {
		if($('tbody.table2>tr').length>2){
		num();
		if(oo){
			 $('input[name="check"]:checked').parent().parent().remove();
		}
		change();
		}
	}
	// 改变table行序号
	function change(){
		$('.num').each(function(i){
		$(this).html(i+1);
		})
	}*/
	// select的值发生变化时
	// $('.select_type').change(function(){
	// 	var v = $(this).val();
	// 	var index = $(this).parent().parent().index();
	// 	var url  = "ajax_project.php";
	// 	$.ajax({
	// 		type: 'GET',
	// 		url: url,
	// 		data:{
	// 			value:v,
	// 		},
	// 		dataType:'json',
	// 		success:function(result){
	// 			$('.input_table').eq(index-1).find(".size").val(eval(result)['size']);
	// 			$('.input_table').eq(index-1).find(".units").val(eval(result)['units']);
	// 			$('.input_table').eq(index-1).find(".currency").val(eval(result)['currency']);
	// 			$('.input_table').eq(index-1).find(".number").val(eval(result)['number']);
	// 		}
	// 	})
	// })

	</script>
        <table width="1200" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="100" align="center" valign="bottom"><p><span class="STYLE8 STYLE3"><a rel="nofollow" target="_blank" href="http://www.ego-buy.com.cn/faqListDetail.do?parent_id=108&id=112">集团简介 | </a><a rel="nofollow" target="_blank" href="http://www.ego-buy.com.cn/faqListDetail.do?parent_id=108&id=143">广告服务</a> | <a rel="nofollow" target="_blank" href="http://www.ego-buy.com.cn/faqListDetail.do?parent_id=108&id=115">版权信息 |</a> <a rel="nofollow" target="_blank" href="http://www.ego-buy.com.cn/faqListDetail.do?parent_id=108&id=113">商家入驻 </a> | <a rel="nofollow" target="_blank" href="http://www.ego-buy.com.cn/faqListDetail.do?parent_id=108&id=114">新闻动态</a></span></p></td>
          </tr>
          <tr>
            <td height="30" align="center"><span class="STYLE3">Copyright © 2014-2015 ego-buy.com.cn All Rights Reserved 粤ICP备15026826号</span></td>
          </tr>
        </table>
</body>
</html>